<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <script>
        function Student (name, age, score) {
            this.name = name;
            this.age = age;
            this.score = score;
        }
        function HashTable() {
           var size = 0;
           var entry = new Object();
           this.hasCode = function (value) {
                var key = value % 97;
                return key;
           };
           this.hasStrCode = function (value) {
                var key = 0;
                for(var i = 0; i< value.length; i++){
                    key += value.charCodeAt(i) - 96;
                }
                return key;
           };
           this.add = function (key, value) {
               entry[key] = value;
               size++;
           };
           this.getValue = function (key) {
               return entry[key];
           };
           this.remove = function (key) {
               delete entry[key];
               size--;
           };
           this.getSize = function () {
               return size;
           };
           this.showAll = function () {
               var values = [];
               for(var key in entry){
                   values.push(entry[key]);
               }
               return values;
           }

       }

        var hash = new HashTable();
        //整数的存取——取模
        /*var data = [2, 6, 14, 8, 7, 4, 5, 12, 28, 45];
        for (var i =0; i < data.length; i++){
           var key = hash.hasCode(data[i]);
           hash.add(key, data[i]);
        }*/

       //字符串的存取——转成ascii

       /* var str = ['kris', 'cat', 'davis', 'andrew'];
        for(var i = 0; i < str.length; i++){
            var key = hash.hasStrCode(str[i]);
            hash.add(key, str[i]);
        }
        var re = hash.showAll();
        console.log(re);*/

       //存取学生信息：姓名：年龄：分数;以姓名作为映射值
        var s1 = new Student('kris', 21, 8);
        var s2 = new Student('cat', 25, 8);
        var key = hash.hasStrCode(s1.name);
        hash.add(key, s1);
        key = hash.hasStrCode(s2.name);
        hash.add(key, s2);

        var re = hash.showAll();
        console.log(re);

        //哈希冲突
        //开发寻址法，重写key的方法
        function hasCode(value) {
            // var key =  定义得到key的方法
            //如果key值存在，则key++/key+random/...
            getvalue[key] != null;//key值存在
        }




    </script>
</body>
</html>